package ehot.mainWindow;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputListener;

public class MainWindow {

	public class MenuPanel extends JPanel implements MouseInputListener{
		private static final long serialVersionUID = -2697846538972160594L;

		MenuPanel() {
			super();
			setCursor(new Cursor(Cursor.HAND_CURSOR));
			setBackground(Color.RED);
			setVisible(true);
			setPreferredSize(new Dimension(400, 20));
			setMaximumSize(new Dimension(400, 20));

			JButton exitButton = new JButton();
			exitButton.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					//MainWindow.getFrame().setVisible(false);
				}});
			add(exitButton, BorderLayout.EAST);

			/*MouseListener mainWindowListener = new MouseListener(this);
			addMouseListener(mainWindowListener);
			addMouseMotionListener(mainWindowListener);
	*/
			addMouseListener(this);
			addMouseMotionListener(this);
		}

		public boolean mouseMove(Event evt, int x, int y) {
			if (getBackground() != Color.BLUE) {
				setBackground(Color.BLUE);
			}
			return true;
		}

		public void mouseClicked(MouseEvent arg0) {}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			if (getBackground() != Color.BLUE) {
				setBackground(Color.BLUE);
			}
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			if (getBackground() != Color.RED) {
				setBackground(Color.RED);
			}
			
		}
		public void mousePressed(MouseEvent arg0) {}
		public void mouseReleased(MouseEvent arg0) {}
		public void mouseDragged(MouseEvent e) {}
		public void mouseMoved(MouseEvent e) {}
	}
	
	private JFrame mainFrame;
	
	public static void main(String[] args) {
        MainWindow window = new MainWindow();
        window.createWindow();
        initTray();
	}

	private static void initTray() {
		final TrayIcon trayIcon;

		if (SystemTray.isSupported()) {

		    SystemTray tray = SystemTray.getSystemTray();
		    Image image = Toolkit.getDefaultToolkit().getImage("resources/logo24.png");
		    
		    MouseListener mouseListener = new MouseListener() {
		                
		        public void mouseClicked(MouseEvent e) {
		            System.out.println("Tray Icon - Mouse clicked!");                 
		        }

		        public void mouseEntered(MouseEvent e) {
		            System.out.println("Tray Icon - Mouse entered!");                 
		        }

		        public void mouseExited(MouseEvent e) {
		            System.out.println("Tray Icon - Mouse exited!");                 
		        }

		        public void mousePressed(MouseEvent e) {
		            System.out.println("Tray Icon - Mouse pressed!");                 
		        }

		        public void mouseReleased(MouseEvent e) {
		            System.out.println("Tray Icon - Mouse released!");                 
		        }
		    };

		    ActionListener exitListener = new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		            System.out.println("Exiting...");
		            System.exit(0);
		        }
		    };
		            
		    PopupMenu popup = new PopupMenu();
		    MenuItem defaultItem = new MenuItem("Exit");
		    defaultItem.addActionListener(exitListener);
		    popup.add(defaultItem);

		    trayIcon = new TrayIcon(image, "Tray Demo", popup);
		    trayIcon.setImageAutoSize(true);

		    ActionListener actionListener = new ActionListener() {
		        public void actionPerformed(ActionEvent e) {
		            trayIcon.displayMessage("Action Event", 
		                "An Action Event Has Been Performed!",
		                TrayIcon.MessageType.INFO);
		        }
		    };
		            
		    trayIcon.setImageAutoSize(true);
		    trayIcon.addActionListener(actionListener);
		    trayIcon.addMouseListener(mouseListener);

		    try {
		        tray.add(trayIcon);
		    } catch (AWTException e) {
		        System.err.println("TrayIcon could not be added.");
		    }

		} else {

		    //  System Tray is not supported

		}
	}

	public Component getFrame(){
		return mainFrame;
	}
	
	private void createWindow(){
				
		mainFrame = new JFrame();
		mainFrame.setBackground(Color.BLUE);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.setUndecorated(true);

		MenuPanel menuPanel = new MenuPanel();
		mainFrame.getContentPane().add(menuPanel, BorderLayout.NORTH);

		JPanel mainPanel = new JPanel(new BorderLayout());
		mainFrame.getContentPane().add(mainPanel, BorderLayout.PAGE_END);
		mainFrame.setMinimumSize(new Dimension(400,400));
		
		mainPanel.setPreferredSize(new Dimension(mainFrame.getSize().width,mainFrame.getSize().height-5));
		mainPanel.setLocation(0, 20);
		mainPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
/*		
		MouseListener mainWindowListener = new MouseListener();
	    mainFrame.addMouseListener(mainWindowListener);
	    mainFrame.addMouseMotionListener(mainWindowListener);
*/
		mainFrame.pack();
		mainFrame.setVisible(true);
	}
}
